# -*- coding: utf-8 -*-
# @Time : 2021/9/9 15:51
# @Author : 阿怪
# @Email : scg@gmail.com
# @File : CRM-ST-TJFX-008.py
# @Project : crm自动化测试项目实战

'''验证线索总计的搜索功能—正确输入搜索信息'''

from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.support.select import Select
from time import sleep
from selenium.webdriver.common.alert import Alert  # 把弹窗封装成类Alert

driver = webdriver.Chrome()
cookie = {'name': 'PHPSESSID', 'value': '0onrpth7nsd6mbi1hfgi99o2b1'} # 获取到cookid
driver.get('http://192.168.1.140/')  # 访问服务器
driver.add_cookie(cookie)
driver.get('http://192.168.1.140/crm3/index.php?m=leads&a=index')
# 进入线索首页
try:
    clue_locator = (By.CSS_SELECTOR,'body > div.container > div.page-header > ul > li:nth-child(1) > a')  #线索
    clue_add_locator = (By.CSS_SELECTOR,'body > div.container > div.row > div:nth-child(1) > div > a')   #添加线索定位器
    clue_checkall_locator = (By.ID,'check_all') #全选线索定位器
    operation_locator = (By.CSS_SELECTOR,'body > div.container > div.row > div:nth-child(1) > ul > li:nth-child(1) > div > a')
     #批量操作定位器

    #先去添加一条线索数据
    driver.find_element(*clue_add_locator).click()
    driver.find_element(By.XPATH,'//*[@id="contacts_name"]').send_keys('欧哥')
    driver.find_element(By.NAME,'submit').click()
    #1.进入线索统计页面
    driver.get('http://192.168.1.140/crm3/index.php?m=leads&a=analytics')

    select_department_locator = driver.find_element(By.ID,'department')   #选择部门定位器
    select_role_locator = driver.find_element(By.ID,'role')    #选择人员定位器
    start_time_locator = (By.ID,'start_time')     #开始日期定位器
    start_time_element = driver.find_element(*start_time_locator)
    end_time_locator = (By.ID,'end_time')       #结束日期
    end_time_element = driver.find_element(*end_time_locator)
    search_locator = (By.CSS_SELECTOR,'#searchForm > ul > li:nth-child(4) > button')    #搜索按钮定位器
    today= time.strftime('%Y-%m-%d')   #获取当前日期

    # 1.第一个下拉框选择办公室
    select = Select(select_department_locator)
    select.select_by_value('1')
    # 2.第二个下拉框选择admin
    select1 = Select(select_role_locator)
    sleep(3)
    select1.select_by_visible_text('CEO-admin')
    #3.第三个下拉框选择今天
    start_time_element.send_keys(today)
    #4.第四个下拉框选择今天
    end_time_element.send_keys(today)
    #5.点击【搜索】
    driver.find_element(*search_locator).click()

    #每条用例都要清理数据。。。。回到线索界面把添加的线索全部删掉，有弹窗！害
    driver.find_element(*clue_locator).click()
    driver.find_element(*clue_checkall_locator).click()
    driver.find_element(*operation_locator).click()
    driver.find_element(By.XPATH,'//*[@id="delete"]').click()
    alert = Alert(driver)   #实例化Alert
    sleep(3)
    alert.accept()
    sleep(3)
    driver.quit()

except Exception as e:
    raise e